<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class account {

	//登录验证
	public function login() {
		if (empty(getPost('username')) || valid::isWicked(getPost('username'))) {
			return util::getValidError('用户名格式错误');
		}

		if (empty(getPost('password')) || valid::isWicked(getPost('password'))) {
			return util::getValidError('密码格式错误');
		}

		$rst = new sqlcon();
		$user = $rst->getInfo('id, name, userid, username, password', 'user', "isuse = 1 and username = '" . getPost('username') . "'");
		if (empty($user)) {
			return util::getValidError('用户未注册');
		}

		$password = util::encrypt(getPost('password'));
		if ($user['password'] != $password) {
			return util::getValidError('密码错误');
		}

		$random = util::getRandomNum(6);

		$rst->addCollect('flag', $random);
		$rst->addCollect('logintime', date('Y-m-d H:i:s'));
		$rst->addCollect('dutystatus', '0');
		$rst->addCollect('dutytime', '');
		$rst->addCollect('id', $user['id']);
		$rst->updateInfo('user');

		$result = util::getSuccess('success');
		$result['token'] = util::encrypt($user['userid'] . '[@#%&]' . $user['username'] . '[@#%&]' . $password . '[@#%&]' . $user['name'] . '[@#%&]' . time() . '[@#%&]' . $random);
		return $result;
	}

	//获取登录者菜单信息
	public function getOwnerInfo() {
		$token = util::decrypt(getServer('HTTP_TOKEN'));
		$temp = explode('[@#%&]', $token);

		$rst = new sqlcon();
		$user = $rst->getInfo('a.userid, a.name, b.code', 'user a, role b', "a.isuse = 1 and b.isuse = 1 and a.roleid = b.roleid and a.username = '" . $temp[1] . "'");
		if (empty($user)) {
			return util::getDataError('用户ID已关闭');
		}

		$result = util::getSuccess('success');
		$result['userid'] = $user['userid'];
		$result['name'] = $user['name'];

		$code = $user['code'];

		$menu = $rst->getList('id, name, grade, mode, url, useto', 'menu', 'isuse = 1 and type = 0', 'sx asc');
		for ($i = count($menu) - 1; $i >= 0; $i--) {
			if (strpos($code, '●' . $menu[$i]['id'] . '●') !== false) {
				$parentMenu = $rst->getList('id, name, icon, useto', 'menu', 'isuse = 1 and type = 1 and grade = 0 and parentid = ' . $menu[$i]['id'], 'sx asc');
				
				for ($m = count($parentMenu) - 1; $m >= 0; $m--) {
					$parentMenu[$m]['icon'] = _HTTP_PATH_ . $parentMenu[$m]['icon'];

					if (strpos($code, '●' . $parentMenu[$m]['id'] . '●') !== false) {
						$childMenu = $rst->getList('id, name, icon, url, useto', 'menu', 'isuse = 1 and type = 1 and grade = 1 and parentid = ' . $parentMenu[$m]['id'], 'sx asc');
					
						for ($n = count($childMenu) - 1; $n >= 0; $n--) {
							$childMenu[$n]['icon'] = _HTTP_PATH_ . $childMenu[$n]['icon'];

							if (strpos($code, '●' . $childMenu[$n]['id'] . '●') !== false) {
								if (!empty($childMenu[$n]['url'])) {
									//获取菜单页面的模块权限
									$permission = '&permission=';
									$list = $rst->getList('b.code', 'user a, operate b', "a.roleid = b.roleid and b.type = 'role' and a.userid = '" . $result['userid'] . "' and b.menuid = '" . $childMenu[$n]['id'] . "'");
									for ($j = 0; $j < count($list); $j++) {
										if ($j == count($list) - 1) {
											$permission .= $list[$j]['code'];
										}
										else {
											$permission .= $list[$j]['code'] . '●';
										}
									}

									//将页面权限拼接到菜单url后面
									$childMenu[$n]['url'] .= '&menuid=' . $childMenu[$n]['id'] . $permission;
								}
							}
							else {
								array_splice($childMenu, $n, 1);
							}
						}

						$parentMenu[$m]['child'] = $childMenu;
					}
					else {
						array_splice($parentMenu, $m, 1);
					}
				}

				$menu[$i]['parent'] = $parentMenu;
			}
			else {
				array_splice($menu, $i, 1);
			}
		}

		//获取该角色的额外菜单权限
		$owner = util::getOperateItem('');

		$result['menu'] = $menu;
		$result['owner'] = $owner;
	    return $result;
	}

	//获取待办事务数量
	public function getWaitWorkCount() {
		$rst = new sqlcon();

		//获取待处理的举报信息数量
		$info = $rst->getInfo('count(*) as num', 'report', "status = 0");

		$result = util::getSuccess('success');
		$result['waitReportCount'] = $info['num'];
		return $result;
	}

	//获取登录者姓名
	public function getOwnerName() {
		if (empty(getPost('userid'))) {
			return util::getValidError('用户ID格式错误');
		}

		$rst = new sqlcon();
		$info = $rst->getInfo('username', 'user', "userid = '" . getPost('userid') . "'");
		if (empty($info)) {
			return util::getDataError('用户ID已关闭');
		}

	    return array_merge(util::getSuccess('success'), $info);
	}

	//修改密码
	public function editPwd() {
		if (empty(getPost('userid'))) {
			return util::getValidError('用户ID格式错误');
		}

		if (empty(getPost('username')) || valid::isWicked(getPost('username'))) {
			return util::getValidError('用户名格式错误');
		}

		if (empty(getPost('oldusername')) || valid::isWicked(getPost('oldusername'))) {
			return util::getValidError('原用户名格式错误');
		}

		if (empty(getPost('newpassword')) || valid::isWicked(getPost('newpassword'))) {
			return util::getValidError('新密码格式错误');
		}

		if (empty(getPost('againpassword')) || valid::isWicked(getPost('againpassword'))) {
			return util::getValidError('再次输入的密码格式错误');
		}

		if (getPost('newpassword') != getPost('againpassword')) {
			return util::getValidError('两次密码输入不一致');
		}

		$rst = new sqlcon();
		$info = $rst->getInfo('id', 'user', "username != '" . getPost('oldusername') . "' and username = '" . getPost('username') . "'");
		if (!empty($info)) {
			return util::getDataError('该用户名已被使用');
		}

		$rst->addCollect('username', getPost('username'));
		$rst->addCollect('password', util::encrypt(getPost('newpassword')));
		$rst->addCollect('userid', getPost('userid'));
		$rst->updateInfo('user');

		return util::getSuccess('success');
	}

	//获取用户列表
	public function getUserList() {
		$isuse = getPost('isuse');
		$keyword = getPost('keyword');

		$queryitem = '';
		if ($isuse != '')
			$queryitem .= ' and isuse = ' . $isuse;
		if ($keyword != '')
			$queryitem .= " and name like '%" . $keyword . "%'";

		$rst = new sqlcon();

		//获取用户总数
		$info = $rst->getInfo('count(*) as num', 'user', 'id != 0' . $queryitem);

		//获取用户列表
	    $user = $rst->getList('id, name, userid, username, roleid, isuse, logintime', 'user', 'id != 0' . $queryitem, 'id asc');
	    for ($i = 0; $i < count($user); $i++) {
	    	if (!empty($user[$i]['roleid'])) {
	    		$role = $rst->getInfo('name', 'role', "isuse = 1 and roleid = '" . $user[$i]['roleid'] . "'");
	    		$user[$i]['role'] = $role['name'];
	    	}
	    	else {
	    		$user[$i]['role'] = '';
	    	}
	    }

	    $result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['userList'] = $user;
		return $result;
	}

	//获取用户信息
	public function getUserInfo() {
		if (empty(getPost('userid'))) {
			return util::getValidError('用户ID格式错误');
		}

		$rst = new sqlcon();
		$info = $rst->getInfo('name, username, isuse', 'user', "userid = '" . getPost('userid') . "'");
		if (empty($info)) {
			return util::getDataError('用户信息不存在');
		}

	    return array_merge(util::getSuccess('success'), $info);
	}

	//保存用户信息
	public function saveUser() {
		if (getPost('flag') != 'add' && getPost('flag') != 'edit') {
			return util::getValidError('保存用户操作异常');
		}

		if (empty(getPost('usermc')) || valid::isWicked(getPost('usermc'))) {
			return util::getValidError('姓名格式错误');
		}

		if (empty(getPost('username')) || valid::isWicked(getPost('username'))) {
			return util::getValidError('账号格式错误');
		}

		if (getPost('status') != '1' && getPost('status') != '0') {
			return util::getValidError('状态格式错误');
		}

		$rst = new sqlcon();

		if (getPost('flag') == 'add') {
			//判断该账号是否被使用
			$info = $rst->getInfo('id', 'user', "username = '" . getPost('username') . "'");
			if (!empty($info)) {
				return util::getDataError('该账号已经被使用');
			}

			$rst->addCollect('userid', util::getUUID());
			$rst->addCollect('name', getPost('usermc'));
			$rst->addCollect('username', getPost('username'));
			$rst->addCollect('isuse', getPost('status'));
			$rst->addCollect('dutystatus', '0');
			$rst->addCollect('dutytime', '');
			$rst->insertInfo('user');
		}
		else {
			if (empty(getPost('userid'))) {
				return util::getValidError('用户ID格式错误');
			}

			//判断该账号是否被使用
			$info = $rst->getInfo('id', 'user', "username = '" . getPost('username') . "' and userid != '" . getPost('userid') . "'");
			if (!empty($info)) {
				return util::getDataError('该账号已经被使用');
			}

			$rst->addCollect('name', getPost('usermc'));
			$rst->addCollect('username', getPost('username'));
			$rst->addCollect('isuse', getPost('status'));
			if (getPost('status') == '0') {
				$rst->addCollect('dutystatus', '0');
				$rst->addCollect('dutytime', '');
			}
			$rst->addCollect('userid', getPost('userid'));
			$rst->updateInfo('user');
		}

		return util::getSuccess('success');
	}

	//获取角色列表
	public function getRoleType() {
		if (!valid::isNum(getPost('typeid'))) {
			return util::getValidError('角色类型格式错误');
		}

		$rst = new sqlcon();
		$roleList = $rst->getList('roleid as value, name as text', 'role', 'isuse = 1 and type = ' . getPost('typeid'), 'sx asc');

		$result = util::getSuccess('success');
		$result['roleList'] = $roleList;

		return $result;
	}

	//修改用户的角色
	public function editRole() {
		if (empty(getPost('userid'))) {
			return util::getValidError('ID格式错误');
		}

		if (empty(getPost('roleid'))) {
			return util::getValidError('角色名称格式错误');
		}

		$rst = new sqlcon();
		$rst->addCollect('roleid', getPost('roleid'));
		$rst->addCollect('userid', getPost('userid'));
		$rst->updateInfo('user');

		return util::getSuccess('success');
	}

	//重置用户的账号
	public function retUsername() {
		if (empty(getPost('userid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();
		$rst->addCollect('username', time());
		$rst->addCollect('userid', getPost('userid'));
		$rst->updateInfo('user');

		return util::getSuccess('success');
	}

	//重置用户的密码
	public function retPassword() {
		if (empty(getPost('userid'))) {
			return util::getValidError('ID格式错误');
		}

		if (empty(getPost('password')) || valid::isWicked(getPost('password'))) {
			return util::getValidError('新密码格式错误');
		}

		$rst = new sqlcon();
		$rst->addCollect('password', util::encrypt(getPost('password')));
		$rst->addCollect('userid', getPost('userid'));
		$rst->updateInfo('user');

		return util::getSuccess('success');
	}

	//获取角色信息
	public function getRoleList() {
		$isuse = getPost('isuse');
		$typeid = getPost('typeid');
		$keyword = getPost('keyword');
		$page = getPost('page', '1');

		$queryitem = '';
		if ($isuse != '')
			$queryitem .= ' and isuse = ' . $isuse;
		if ($typeid != '')
			$queryitem .= ' and type = ' . $typeid;
		if ($keyword != '')
			$queryitem .= " and name like '%" . $keyword . "%'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';

		$rst = new sqlcon();

		//获取角色总数
		$info = $rst->getInfo('count(*) as num', 'role', 'id != 0' . $queryitem);

		$role = $rst->getList('id, roleid, name, type, code, isuse, sx', 'role', 'id != 0' . $queryitem, 'sx asc' . $limititem);
	    for ($i = 0; $i < count($role); $i++) {
	    	$role[$i]['type'] = util::getArrayValue($role[$i]['type'], array('管理岗', '运营岗', '研发岗', '试用岗'));

	    	$role[$i]['code'] = str_replace('●●', ',', $role[$i]['code']);
	    	$role[$i]['code'] = str_replace('●', '', $role[$i]['code']);
	    }

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['roleList'] = $role;
	    $result['rowCount'] = 50;
		return $result;
	}

	//删除角色
	public function deleteRole() {
		if (empty(getPost('roleid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();
		//判断是否仍有用户使用
		$role = $rst->getInfo('id', 'user', "roleid = '" . getPost('roleid') . "'");
		if (!empty($role)) {
			return util::getValidError('该角色还有用户在使用');
		}

		$rst->addCollect('roleid', getPost('roleid'));
        $rst->deleteInfo('role');

        return util::getSuccess('success');
	}

	//获取角色的所有菜单项
	public function getRoleMenu() {
		$rst = new sqlcon();

		$menu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 0', 'sx asc');
		for ($i = 0; $i < count($menu); $i++) {
			$parentMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and parentid = ' . $menu[$i]['id'], 'sx asc');

			for ($m = 0; $m < count($parentMenu); $m++) {
				$childMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and grade = 1 and parentid = ' . $parentMenu[$m]['id'], 'sx asc');

				$parentMenu[$m]['child'] = $childMenu;
			}

			$menu[$i]['parent'] = $parentMenu;
		}

		$result = util::getSuccess('success');
	    $result['menuList'] = $menu;
		return $result;
	}

	//根据角色ID获取角色的所有菜单项
	public function getRoleMenuById() {
		$roleid = getPost('roleid');

		$rst = new sqlcon();
		//获取角色基本信息
		$role = $rst->getInfo('roleid, name, type, code, isuse, sx', 'role', "roleid = '" . $roleid . "'");

		//获取系统菜单结构
		$menu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 0', 'sx asc');
		for ($i = 0; $i < count($menu); $i++) {
			$parentMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and parentid = ' . $menu[$i]['id'], 'sx asc');

			for ($m = 0; $m < count($parentMenu); $m++) {
				$childMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and grade = 1 and parentid = ' . $parentMenu[$m]['id'], 'sx asc');

				$parentMenu[$m]['child'] = $childMenu;
			}

			$menu[$i]['parent'] = $parentMenu;
		}

		$result = util::getSuccess('success');
	    $result['menuList'] = $menu;
	    $result['role'] = $role;
		return $result;
	}

	//保存角色信息
	public function saveRole() {
		if (getPost('flag') != 'add' && getPost('flag') != 'edit') {
			return util::getValidError('保存角色操作异常');
		}

		if (empty(getPost('rolemc')) || valid::isWicked(getPost('rolemc'))) {
			return util::getValidError('角色名称格式错误');
		}

		if (!valid::isNum(getPost('roleType'))) {
			return util::getValidError('角色类型格式错误');
		}

		if (!valid::isNum(getPost('sx'))) {
			return util::getValidError('顺序格式错误');
		}

		if (empty(getPost('code'))) {
			return util::getValidError('菜单权限不能为空');
		}

		if (getPost('status') != '1' && getPost('status') != '0') {
			return util::getValidError('状态格式错误');
		}

		$rst = new sqlcon();

		if (getPost('flag') == 'add') {
			$rst->addCollect('roleid', util::getUUID());
			$rst->addCollect('name', getPost('rolemc'));
			$rst->addCollect('type', getPost('roleType'));
			$rst->addCollect('code', getPost('code'));
			$rst->addCollect('isuse', getPost('status'));
			$rst->addCollect('sx', getPost('sx'));
			$rst->insertInfo('role');
		}
		else {
			if (empty(getPost('roleid'))) {
				return util::getValidError('ID格式错误');
			}

			$rst->addCollect('name', getPost('rolemc'));
			$rst->addCollect('type', getPost('roleType'));
			$rst->addCollect('code', getPost('code'));
			$rst->addCollect('isuse', getPost('status'));
			$rst->addCollect('sx', getPost('sx'));
			$rst->addCollect('roleid', getPost('roleid'));
			$rst->updateInfo('role');
		}

		return util::getSuccess('success');
	}

	//获取菜单信息
	public function getMenuList() {
		$isuse = getPost('isuse');
		$menutype = getPost('menutype');
		$menugrade = getPost('menugrade');
		$useto = getPost('useto');
		$keyword = getPost('keyword');
		$page = getPost('page', '1');

		$queryitem = '';
		if ($isuse != '')
			$queryitem .= ' and isuse = ' . $isuse;
		if ($useto != '')
			$queryitem .= ' and useto = ' . $useto;
		if ($menutype != '')
			$queryitem .= ' and type = ' . $menutype;
		if ($menugrade != '')
			$queryitem .= ' and grade = ' . $menugrade;
		if ($keyword != '')
			$queryitem .= " and name like '%" . $keyword . "%'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';

		$rst = new sqlcon();

		//获取菜单总数
		$info = $rst->getInfo('count(*) as num', 'menu', 'id != 0' . $queryitem);

		$menu = $rst->getList('id, name, grade, type, mode, icon, useto, isuse, sx', 'menu', 'id != 0' . $queryitem, 'sx asc' . $limititem);
	    for ($i = 0; $i < count($menu); $i++) {
	    	$menu[$i]['grade'] = $menu[$i]['grade'] == '0' ? '父菜单' : '功能菜单';
	    	$menu[$i]['type'] = $menu[$i]['type'] == '0' ? '顶部导航' : '左侧菜单';
	    	$menu[$i]['useto'] = $menu[$i]['useto'] == '0' ? '用于pc/wap端' : '用于pc端';
	    	$menu[$i]['status'] = $menu[$i]['isuse'] == '1' ? '启用' : '<span class="tc1">关闭</span>';
	    	
	    	if (!empty($menu[$i]['icon'])) {
	    		$menu[$i]['icon'] = _HTTP_PATH_ . $menu[$i]['icon'];
	    	}
	    	
	    	if ($menu[$i]['mode'] == 'blank')
				$menu[$i]['mode'] = '新窗口';
			else if ($menu[$i]['mode'] == 'link')
				$menu[$i]['mode'] = '原窗跳转';
			else
				$menu[$i]['mode'] = '-';
	    }

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['menuList'] = $menu;
	    $result['rowCount'] = 50;
		return $result;
	}

	//保存菜单信息
	public function saveMenu() {
		if (getPost('flag') != 'add' && getPost('flag') != 'edit') {
			return util::getValidError('保存菜单操作异常');
		}

		if (empty(getPost('menumc')) || valid::isWicked(getPost('menumc'))) {
			return util::getValidError('菜单名称格式错误');
		}

		if (!valid::isNum(getPost('menusx'))) {
			return util::getValidError('顺序格式错误');
		}

		if (getPost('menutype') != '0' && getPost('menutype') != '1') {
			return util::getValidError('类型格式错误');
		}

		if (getPost('menugrade') != '0' && getPost('menugrade') != '1') {
			return util::getValidError('等级格式错误');
		}

		if (getPost('useto') != '1' && getPost('useto') != '0') {
			return util::getValidError('用途格式错误');
		}

		if (getPost('status') != '1' && getPost('status') != '0') {
			return util::getValidError('状态格式错误');
		}

		if (getPost('menutype') == '0' && getPost('menugrade') == '1') {
			if (getPost('menumode') != 'blank' && getPost('menumode') != 'link') {
				return util::getValidError('打开方式格式错误');
			}

			if (empty(getPost('menuurl'))) {
				return util::getValidError('链接不能为空');
			}
		}
		else if (getPost('menutype') == '1' && getPost('menugrade') == '0') {
			if (empty(getPost('menuparent'))) {
				return util::getValidError('请选择上一级菜单');
			}

			if (empty(getPost('picText'))) {
				return util::getValidError('请上传图片');
			}
		}
		else if (getPost('menutype') == '1' && getPost('menugrade') == '1') {
			if (empty(getPost('menuparent'))) {
				return util::getValidError('请选择上一级菜单');
			}

			if (getPost('menumode') != 'blank' && getPost('menumode') != 'link') {
				return util::getValidError('打开方式格式错误');
			}

			if (empty(getPost('menuurl'))) {
				return util::getValidError('链接不能为空');
			}

			if (empty(getPost('picText'))) {
				return util::getValidError('请上传图片');
			}
		}

		$rst = new sqlcon();
		$fileName = $this->saveMenuIcon();

		if (getPost('flag') == 'add') {
			$rst->addCollect('parentid', getPost('menuparent') != '' ? getPost('menuparent') : '0');
			$rst->addCollect('name', getPost('menumc'));
			$rst->addCollect('grade', getPost('menugrade'));
			$rst->addCollect('type', getPost('menutype'));
			$rst->addCollect('mode', getPost('menumode'));
			$rst->addCollect('icon', $fileName != '' ? 'upload/menu/' . $fileName : '');
			$rst->addCollect('url', getPost('menuurl'));
			$rst->addCollect('useto', getPost('useto'));
			$rst->addCollect('isuse', getPost('status'));
			$rst->addCollect('sx', getPost('menusx'));
			$rst->insertInfo('menu');
		}
		else {
			if (empty(getPost('menuid'))) {
				return util::getValidError('ID格式错误');
			}

			$rst->addCollect('parentid', getPost('menuparent') != '' ? getPost('menuparent') : '0');
			$rst->addCollect('name', getPost('menumc'));
			$rst->addCollect('mode', getPost('menumode'));
			$rst->addCollect('icon', $fileName != '' ? 'upload/menu/' . $fileName : '');
			$rst->addCollect('url', getPost('menuurl'));
			$rst->addCollect('useto', getPost('useto'));
			$rst->addCollect('isuse', getPost('status'));
			$rst->addCollect('sx', getPost('menusx'));
			$rst->addCollect('id', getPost('menuid'));
			$rst->updateInfo('menu');
		}

		return util::getSuccess('success');
	}

	//保存菜单图片
	protected function saveMenuIcon() {
		$fileName = '';

		if (!empty(getPost('picText'))) {
			if (strpos(getPost('picText'), 'base64,') !== false) {
				$base64 = explode('base64,', getPost('picText'));
				$img = base64_decode($base64[1]);

				$fileName = util::getUUID();

				if (strpos($base64[0], 'image/jpeg') !== false || 
					strpos($base64[0], 'image/jpg')  !== false) {
					$fileName .= '.jpg';
				}
				else if (strpos($base64[0], 'image/png') !== false) {
					$fileName .= '.png';
				}
				else {
					$fileName .= '.jpg';
				}

				file_put_contents(_IMGS_PATH_ . 'upload/menu/' . $fileName, $img);
			}
			else {
				$fileName = getPost('picText');
			}
		}

		return $fileName;
	}

	//获取菜单信息
	public function getMenuInfo() {
		if (empty(getPost('menuid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();
		$menu = $rst->getInfo('parentid, name, grade, type, mode, icon, url, useto, isuse, sx', 'menu', 'id = ' . getPost('menuid'));
		if (empty($menu)) {
			return util::getDataError('获取菜单信息失败');
		}

		if(!empty($menu['icon'])) {
			$menu['icon'] = explode('/', $menu['icon'])[2];
			$menu['iconPath'] = _HTTP_PATH_;
		}

		if ($menu['type'] == '1' && $menu['grade'] == '0') {
			$parentMenu = $rst->getList('id as value, name as text', 'menu', 'isuse = 1 and type = 0 and grade = 0', 'sx asc');
			$menu['parentMenu'] = $parentMenu;
		}
		else if ($menu['type'] == '1' && $menu['grade'] == '1') {
			$parentMenu = $rst->getList('id as value, name as text', 'menu', 'isuse = 1 and type = 1 and grade = 0', 'sx asc');
			$menu['parentMenu'] = $parentMenu;
		}
		else {
			$menu['parentMenu'] = '';
		}

		return array_merge(util::getSuccess('success'), $menu);
	}

	//获取顶层父菜单
	public function getTopParentMenu() {
		$rst = new sqlcon();
		$menuList = $rst->getList('id as value, name as text', 'menu', 'isuse = 1 and type = 0 and grade = 0', 'sx asc');

		$result = util::getSuccess('success');
		$result['menuList'] = $menuList;

		return $result;
	}

	//获取左侧父菜单
	public function getLeftParentMenu() {
		$rst = new sqlcon();
		$menuList = $rst->getList('id as value, name as text', 'menu', 'isuse = 1 and type = 1 and grade = 0', 'sx asc');

		$result = util::getSuccess('success');
		$result['menuList'] = $menuList;

		return $result;
	}

	//删除菜单
	public function deleteMenu() {
		if (empty(getPost('menuid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();
		//判断其下是否仍有子菜单
		$menu = $rst->getInfo('id', 'menu', 'parentid = ' . getPost('menuid'));
		if (!empty($menu)) {
			return util::getValidError('该菜单下面仍有子菜单，不能删除');
		}

		//删除菜单图标
		$menu = $rst->getInfo('icon', 'menu', 'id = ' . getPost('menuid'));
		if (empty($menu)) {
			return util::getDataError('获取菜单信息失败');
		}

		if (!empty($menu['icon'])) {
			$file_path = _IMGS_PATH_ . $menu['icon'];
			if (file_exists($file_path)) {
				unlink($file_path);
			}
		}
		
		//删除菜单数据
        $rst->addCollect('id', getPost('menuid'));
        $rst->deleteInfo('menu');

        return util::getSuccess('success');
	}

	//获取菜单的操作项列表
	public function getOperateList() {
		$menuid = getPost('menuid');

		$queryitem = '';
		if ($menuid != '')
			$queryitem .= " and menuid = '" . $menuid . "'";

		$rst = new sqlcon();

		//获取操作项总数
		$info = $rst->getInfo('count(*) as num', 'operate', "type = 'menu'" . $queryitem);

		//获取操作项列表
	    $list = $rst->getList('id, operateid, code, name', 'operate', "type = 'menu'" . $queryitem, 'id asc');

	    $result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['operateList'] = $list;
		return $result;
	}

	//保存操作项信息
	public function saveOperate() {
		if (!valid::isNum(getPost('menuid'))) {
			return util::getValidError('菜单ID格式错误');
		}

		if (empty(getPost('code')) || valid::isWicked(getPost('code'))) {
			return util::getValidError('代号格式错误');
		}

		if (empty(getPost('name')) || valid::isWicked(getPost('name'))) {
			return util::getValidError('名称格式错误');
		}

		$rst = new sqlcon();

		//判断该代号在同一菜单下的操作项中是否被使用
		$info = $rst->getInfo('id', 'operate', "menuid = '" . getPost('menuid') . "' and code = '" . getPost('code') . "'");
		if (!empty($info)) {
			return util::getDataError('该代号已经被使用');
		}

		$rst->addCollect('operateid', time());
		$rst->addCollect('roleid', '');
		$rst->addCollect('menuid', getPost('menuid'));
		$rst->addCollect('type', 'menu');
		$rst->addCollect('code', getPost('code'));
		$rst->addCollect('name', getPost('name'));
		$rst->insertInfo('operate');

		return util::getSuccess('success');
	}

	//删除操作项
	public function deleteOperate() {
		if (empty(getPost('operateid'))) {
			return util::getValidError('操作项ID格式错误');
		}

		$rst = new sqlcon();
		//判断该操作项是否仍有角色在使用
		$info = $rst->getInfo('id', 'operate', "operateid = '" . getPost('operateid') . "' and type = 'role'");
		if (!empty($info)) {
			return util::getValidError('该操作项还有角色在使用');
		}

		$rst->addCollect('operateid', getPost('operateid'));
        $rst->deleteInfo('operate');

        return util::getSuccess('success');
	}

	//获取角色的操作项权限
	public function getRoleOperate() {
		if (empty(getPost('roleid'))) {
			return util::getValidError('角色ID格式错误');
		}

		$rst = new sqlcon();
		$info = $rst->getInfo('code', 'role', "roleid = '" . getPost('roleid') . "'");
		if (empty($info)) {
			return util::getDataError('角色ID有误');
		}
		$code = $info['code'];

		//获取该角色的菜单列表与权限
		$menu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 0', 'sx asc');
		for ($i = count($menu) - 1; $i >= 0; $i--) {
			if (strpos($code, '●' . $menu[$i]['id'] . '●') !== false) {
				$parentMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and grade = 0 and parentid = ' . $menu[$i]['id'], 'sx asc');
				
				for ($m = count($parentMenu) - 1; $m >= 0; $m--) {
					if (strpos($code, '●' . $parentMenu[$m]['id'] . '●') !== false) {
						$childMenu = $rst->getList('id, name', 'menu', 'isuse = 1 and type = 1 and grade = 1 and parentid = ' . $parentMenu[$m]['id'], 'sx asc');
					
						for ($n = count($childMenu) - 1; $n >= 0; $n--) {
							if (strpos($code, '●' . $childMenu[$n]['id'] . '●') !== false) {
								//获取该菜单下的操作项列表
								$operate = $rst->getList('operateid, menuid, code, name', 'operate', "type = 'menu' and menuid = '" . $childMenu[$n]['id'] . "'", 'id asc');
								$childMenu[$n]['operateMenu'] = $operate;

								//获取该菜单与当前角色下的操作项列表，并组装成权限字符串
								$operate = $rst->getList('operateid, menuid, code, name', 'operate', "type = 'role' and roleid = '" . getPost('roleid') . "' and menuid = '" . $childMenu[$n]['id'] . "'", 'id asc');
								$operateRole = '';
								for ($j = 0; $j < count($operate); $j++) {
									$operateRole .= '●' . $operate[$j]['code'] . '●';
								}
								$childMenu[$n]['operateRole'] = $operateRole;
								$childMenu[$n]['operateList'] = $operate;
							}
							else {
								array_splice($childMenu, $n, 1);
							}
						}

						$parentMenu[$m]['child'] = $childMenu;
					}
					else {
						array_splice($parentMenu, $m, 1);
					}
				}

				$menu[$i]['parent'] = $parentMenu;
			}
			else {
				array_splice($menu, $i, 1);
			}
		}

		//获取该角色的额外菜单权限
		$ownerMenu = $rst->getList('operateid, menuid, code, name', 'operate', "type = 'role' and roleid = '" . getPost('roleid') . "' and menuid = ''", 'id asc');
		$ownerRole = '';
		for ($i = 0; $i < count($ownerMenu); $i++) {
			$ownerRole .= '●' . $ownerMenu[$i]['operateid'] . '●';
		}

		$result = util::getSuccess('success');
		$result['operate'] = $menu;
		$result['ownerMenu'] = $ownerMenu;
		$result['ownerRole'] = $ownerRole;
		return $result;
	}

	//保存角色的操作项权限
	public function saveRoleOperate() {
		if (empty(getPost('roleid'))) {
			return util::getValidError('角色ID格式错误');
		}

		$rst = new sqlcon();

		//删除该角色原来的操作项权限
		$rst->addCollect('roleid', getPost('roleid'));
		$rst->deleteInfo('operate');

		if (!empty(getPost('operateList'))) {
			$operateList = json_decode(getPost('operateList'), true)[0];

			for ($i = 0; $i < count($operateList); $i++) {
				$rst->clearCollect();
				$rst->addCollect('operateid', $operateList[$i]['operateid']);
				$rst->addCollect('roleid', getPost('roleid'));
				$rst->addCollect('menuid', $operateList[$i]['menuid']);
				$rst->addCollect('type', 'role');
				$rst->addCollect('code', $operateList[$i]['code']);
				$rst->addCollect('name', $operateList[$i]['name']);
				$rst->insertInfo('operate');
			}
		}

		return util::getSuccess('success');
	}

}